<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>D3</title>
	<script src="./js/d3.v6.js"></script>
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script src="./js/index.min.js"></script>
    <script>
        
    maxMinDf=null;
    var df;
    maxMinJson=null;
    //###########################################################
    dateRangeStart=new dfd.date_range({"start":'1997-01-01',period:250, freq:'M'})
    dateRangeEnd=new dfd.date_range({"start":'1997-02-01',period:251, freq:'M'})
    
    dateRangeStart=new dfd.Series(dateRangeStart,{columns:['StartDate']})
    dateRangeEnd=new dfd.Series(dateRangeEnd,{columns:['EndDate']})
    
    dateRangeStart=dateRangeStart.apply(function(e){return e.split(" ")[0]})
    dateRangeEnd=dateRangeEnd.apply(function(e){return e.split(" ")[0]})

    df1 = new dfd.DataFrame(dateRangeStart.data)
    df2 = new dfd.DataFrame(dateRangeEnd.data)

    com_df = dfd.concat({ df_list: [df1, df2], axis: 1 })
    com_df.columns=['StartDate','EndDate']

    function dateGreaterThan(dateString1,dateString2){
        return new Date(dateString1)>= new Date(dateString2)
    }
    gt=dateGreaterThan;    
    //#############################################################
   
    
    //读取csv文件
    d3.csv('./data/temperature_daily.csv',
    function(d){
        df = new dfd.DataFrame(d);
        //df.print()
        dataPromise = getMaxMinJson(df)
        dataPromise.then((e)=>{maxMinJson=JSON.parse(e)})
        //console.log(data)

    }) 
    function getMaxMinJson(df){
        var dateS = com_df.col_data[0];
        var dateE = com_df.col_data[1];
        var date = df.col_data[0];
        var max_t = df.col_data[1];    
        var min_t = df.col_data[2];
        maxArray=[];
        minArray=[];
        monthArray=[];
        yearArray=[];
        j=0;
        for(var i = 0;i<dateE.length;i++){
            if(j>=date.length)
            break;
            max=parseInt(max_t[j]);
            min=parseInt(min_t[j]);
            //console.log(i+':'+max);
            while((gt(date[j],dateS[i])&&(gt(dateE[i],date[j])))){
                //console.log(date[j]+'-'+dateS[i]+'-'+dateE[i])
                //console.log(gt(dateE[i],date[j]))
                
                if(max<parseInt(max_t[j])){
                    max=parseInt(max_t[j]);
                }
                if(min>parseInt(min_t[j])){
                    min=parseInt(min_t[j]);
                }
                j++;
            }
            yearArray.push(dateS[i].split('/')[0]);
            monthArray.push(dateS[i].split('/')[1]);
            maxArray.push(max);
            minArray.push(min);
            //console.log(i+':'+max);
        }
        //组装新的DF：['year','month','max','min']
        maxDf = new dfd.DataFrame(maxArray);
        minDf = new dfd.DataFrame(minArray);
        monthDf = new dfd.DataFrame(monthArray);
        yearDf = new dfd.DataFrame(yearArray);
        maxMinDf = dfd.concat({ df_list: [yearDf,monthDf,maxDf, minDf], axis: 1 });
        maxMinDf.columns=['year','month','max','min'];
        //转为JSON格式
        return maxMinDf.to_json();//异步

    }



        






    </script>
    <script>

    </script>
</head>
<body>
<div id="my_dataviz"></div>


</body>
<script>
    
</script>
</html>